package com.kfm.pm;

import java.util.Iterator;
import java.util.Stack;
import java.util.function.Consumer;

public class StackExample {

    public static void main(String[] args) {
        Stack stack = new Stack();
        System.out.println(stack.push(1)); // 1
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop()); // 4 删除并返回栈顶元素
        System.out.println(stack.peek()); // 3 获取栈顶元素但不删除
        System.out.println(stack.search(2)); // 从1开始返回元素位置
        // 迭代器输出
//        Iterator iterator = stack.iterator();
//        while (iterator.hasNext()) {
//            System.out.println(iterator.next());
//        }
        // 增强for循环
//        for (Object object : stack) {
//            System.out.println(object);
//        }
        // forEach() 方法 匿名类
        stack.forEach(new Consumer() {
            @Override
            public void accept(Object object) {
                System.out.println(object);
            }
        });
        for (int i = 0; i < stack.size(); i++) {
            System.out.println(stack.get(i));
        }
        // System.out.println(stack.get(1)); // 访问索引处的元素，从0开始

    }
}
